package com.irdstudio.efp.nls.api.rest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.irdstudio.basic.framework.core.constant.PubConstant;
import com.irdstudio.efp.console.common.ConsoleConstant;
import com.irdstudio.efp.limit.service.vo.LmtPrdContVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.irdstudio.efp.ctr.service.facade.CtrLoanContService;
import com.irdstudio.efp.ctr.service.vo.CtrLoanContVO;
import com.irdstudio.efp.e4a.service.facade.SRoleDataRuleService;
import com.irdstudio.basic.framework.core.constant.ApplicationConstance;
import com.irdstudio.basic.framework.core.constant.DataRuleType;
import com.irdstudio.basic.framework.core.constant.ResponseData;
import com.irdstudio.basic.framework.core.util.StringUtil;
import com.irdstudio.basic.framework.web.controller.AbstractController;
import com.irdstudio.efp.limit.common.LimitConstant;
import com.irdstudio.efp.limit.service.facade.LmtPrdContService;

@RestController
@RequestMapping("/api")
public class CtrLoanContController extends AbstractController  {
	
	@Autowired
	@Qualifier("ctrLoanContService")
	private CtrLoanContService ctrLoanContService;
	
	@Autowired
	@Qualifier("lmtPrdContService")
	private LmtPrdContService lmtPrdContService;
	@Autowired
	@Qualifier("sRoleDataRuleService")
	private SRoleDataRuleService sRoleDataRuleService;
	
	/**
	 * 列表数据查询-------------
	 * @return
	 */
	@RequestMapping(value="/ctr/loan/conts", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<CtrLoanContVO>> queryCtrLoanContAll(@RequestBody CtrLoanContVO vo) {		
		List<CtrLoanContVO> outputVo = null;
		// 根据用户角色获取用户权限方案
		Map<String,String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		vo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PRD_CODE));
		DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
		setUserInfoToVO(vo);
		// 根据权限方案调用不同的方案进行数据查询
		switch (dataRuleType) {
		case Owner:
			outputVo = ctrLoanContService.queryAllOwner(vo);
			break;
		case CurrOrg:
			outputVo = ctrLoanContService.queryAllCurrOrg(vo);
			break;
		case CurrDownOrg:
			outputVo = ctrLoanContService.queryAllCurrDownOrg(vo);
			break;
		case CurrOwnerPrd:
		    outputVo = ctrLoanContService.queryAllCurrOwnerPrd(vo);
		    break;
		default:
			outputVo = ctrLoanContService.queryAllOwner(vo);
			break;
		}
		if (Objects.nonNull(outputVo)) {
			addEleToOutputVo(outputVo);
		}
		return getResponseData(outputVo);
	}

	private void addEleToOutputVo(List<CtrLoanContVO> outputVo) {
		for (CtrLoanContVO item : outputVo) {
			if (ConsoleConstant.CONT_TYPE_01.equals(item.getContType())) {
				item.setStartContDate(getCreditDate(item.getLmtContNo())==null?"":getCreditDate(item.getLmtContNo()).getStartDate());
				item.setEndContDate(getCreditDate(item.getLmtContNo())==null?"":getCreditDate(item.getLmtContNo()).getExpireDate());
			} else if (ConsoleConstant.CONT_TYPE_02.equals(item.getContType())) {
				item.setStartContDate(item.getContStartDt());
				item.setEndContDate(item.getContEndDt());
			}
		}
	}

	private LmtPrdContVO getCreditDate(String lmtContNo) {
		logger.info("开始查询授信协议表，得到授信结束日期。授信协议号为：" + lmtContNo);
		try {
			LmtPrdContVO lmtPrdContVO = new LmtPrdContVO();
			lmtPrdContVO.setLmtContNo(lmtContNo);
			LmtPrdContVO queryRs = lmtPrdContService.queryByPk(lmtPrdContVO);
			if (Objects.nonNull(queryRs)) {
				return queryRs;
			} else {
				logger.info("查询结果为空！");
			}
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return null;
	}

	/**
	 * 根据主键查询详情
	 * @return
	 */
	@RequestMapping(value="/ctr/loan/cont/{contNo}", method=RequestMethod.GET)
	public @ResponseBody ResponseData<CtrLoanContVO> queryByPk(@PathVariable("contNo") String contNo) {		
		CtrLoanContVO inVo = new CtrLoanContVO();
				inVo.setContNo(contNo);
		CtrLoanContVO outputVo = ctrLoanContService.queryByPk(inVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据条件查询合同
	 * @return
	 */
	@RequestMapping(value="/ctr/loan/cont/condition", method=RequestMethod.POST)
	public @ResponseBody ResponseData<CtrLoanContVO> queryByPk(@RequestBody CtrLoanContVO ctrLoanContVO) {		
		CtrLoanContVO inVo = new CtrLoanContVO();
				inVo = ctrLoanContService.queryByCondition(ctrLoanContVO);
		return getResponseData(inVo);
		
	}
	
	
	
	/**
	 * 根据主键删除信息
	 * @return
	 */
	@RequestMapping(value="/ctr/loan/cont", method=RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody CtrLoanContVO inCtrLoanContVo) {		
		int outputVo = ctrLoanContService.deleteByPk(inCtrLoanContVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键更新信息
	 * @param inCtrLoanContVo
	 * @return
	 */
	@RequestMapping(value="/ctr/loan/cont", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody CtrLoanContVO inCtrLoanContVo) {		
		int outputVo = ctrLoanContService.updateByPk(inCtrLoanContVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 新增数据
	 * @param inCtrLoanContVo
	 * @return
	 */
	@RequestMapping(value="/ctr/loan/cont", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertCtrLoanCont(@RequestBody CtrLoanContVO inCtrLoanContVo) {
		int outputVo = ctrLoanContService.insertCtrLoanCont(inCtrLoanContVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 额度冻结合同列表数据查询
	 * @return
	 */
	@RequestMapping(value="/ctr/loan/contSerno", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<CtrLoanContVO>> queryCtrLoanContBySerno(
			@RequestBody CtrLoanContVO vo) {		
		//vo.setContState(ContConstant.CONT_STATUS_200);
		if (Objects.isNull(vo)||StringUtil.isEmpty(vo.getLmtContNo())) {
			return getResponseData(null);
		}
	    List<CtrLoanContVO> outputVo = ctrLoanContService.queryFrozenListByCondition(vo);
		return getResponseData(outputVo);
		
	}
	
	
	/**
	 * 额度台账合同列表数据查询
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="/ctr/loan/contLmtContNo", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<CtrLoanContVO>> queryCtrLoanContByLmtContNo(
			@RequestBody CtrLoanContVO vo) throws Exception {
		//根据授信协议查询， 是否有查询额度台账合同 的数据权限
		//查询是否有数据权限
		Map<String, String> key = new HashMap<>();
		key.put("lmtContNo", vo.getLmtContNo());

		boolean checkRLT = this.checkOptionalAuthority(key, LimitConstant.URL_DATA_PERMISS_LMT_PRD_CONT, this.lmtPrdContService);
		if(!checkRLT)
			return ResponseData.create(null, ApplicationConstance.NO_AUTHORITY, ApplicationConstance.NO_AUTHORITY_MESSAGE, false);
	    List<CtrLoanContVO> outputVo = ctrLoanContService.queryListByCondition(vo);
		return getResponseData(outputVo);
		
	}
	
	
	
	/**
	  * 查询借款合同信息
	  * @return
	  * @author qiuyf
	  * @since 2019年1月8日 下午10:07:57
	  * @version 1.0
	 * @throws Exception 
	 */
	@RequestMapping(value="/qry/ctrLoanCont/by", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<CtrLoanContVO>> getCtrLoanCont(@RequestBody CtrLoanContVO vo) {
		List<CtrLoanContVO> outputVo = ctrLoanContService.queryAllByCusId(vo);
		if (Objects.nonNull(outputVo)) {
			addEleToOutputVo(outputVo);
		}
		return getResponseData(outputVo);
	}

	@RequestMapping(value="/ctr/loan/creditCont", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<CtrLoanContVO>> queryCreditCont(@RequestBody CtrLoanContVO vo) {
	    List<CtrLoanContVO> outputVo = ctrLoanContService.queryCreditContByPage(vo);
		return getResponseData(outputVo);

	}
}
